#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>

int fd;


/* init ad7192*/
int InitAD7192(void)
{
	
	int i;
	int count;

	//write config register
	unsigned char next_config_write[1] = {0x10};
	unsigned char write_config[3] = {0x00,0x02,0x10};

	count = write(fd,&next_config_write[0],sizeof(unsigned char));
	if(count < 0)
	{
		printf("write next_config data error!\n"); 
		return -1;
	}
	
	usleep(1);
		
	for(i = 0; i < 3; i++)
	{
		count = write(fd,&write_config[i],sizeof(unsigned char));
		if(count < 0)
		{		
			printf("write config data error!\n"); 
			return -1;
		}

		usleep(1);
	}

	/*write mode register*/

	unsigned char next_mode_write[1] = {0x08};
	unsigned char write_mode0[3] = {0x88,0x00,0x01};
	unsigned char write_mode1[3] = {0xa8,0x00,0x01};
	unsigned char write_mode2[3] = {0x08,0x00,0x01};

	count = write(fd,&next_mode_write[0],sizeof(unsigned char));
	if(count < 0)
	{
		printf("write next_config data error!\n"); 
		return -1;
	}
	
	usleep(1);
		
	for(i = 0; i < 3; i++)
	{
		count = write(fd,&write_mode0[i],sizeof(unsigned char));
		if(count < 0)
		{		
			printf("write config data error!\n"); 
			return -1;
		}

		usleep(1);
	}

	count = write(fd,&next_mode_write[0],sizeof(unsigned char));
	if(count < 0)
	{
		printf("write next_config data error!\n"); 
		return -1;
	}
	
	usleep(1);	
	for(i = 0; i < 3; i++)
	{
		count = write(fd,&write_mode1[i],sizeof(unsigned char));
		if(count < 0)
		{		
			printf("write config data error!\n"); 
			return -1;
		}

		usleep(1);
	}
	
	count = write(fd,&next_mode_write[0],sizeof(unsigned char));
	if(count < 0)
	{
		printf("write next_config data error!\n"); 
		return -1;
	}
	
	usleep(1);
	for(i = 0; i < 3; i++)
	{
		count = write(fd,&write_mode2[i],sizeof(unsigned char));
		if(count < 0)
		{		
			printf("write config data error!\n"); 
			return -1;
		}

		usleep(1);
	}

	return 0;
}

/*** read register ad7192 ***/
int ReadAD7192(void)
{


	int count;
	int i;
	//read config register
	unsigned char next_config_read[1] = {0x50};
	unsigned char read_config[3] = {0x00,0x00,0x00};

	count = write(fd,&next_config_read[0],sizeof(unsigned char));
	if(count < 0)
	{
		printf("write next_config data error!\n"); 
		return -1;
	}
	
	usleep(1);
		
	for(i = 0; i < 3; i++)
	{
		count = read(fd,&read_config[i],sizeof(unsigned char));
		if(count < 0)
		{		
			printf("write config data error!\n"); 
			return -1;
		}

		usleep(1);
		printf("%2x \t",read_config[i]);
	}

	printf("\n");

	//read mode register
	unsigned char next_mode_read[1] = {0x48};
	unsigned char read_mode[3] = {0x00,0x00,0x00};

	count = write(fd,&next_mode_read[0],sizeof(unsigned char));
	if(count < 0)
	{
		printf("write next_config data error!\n"); 
		return -1;
	}
	
	usleep(1);
		
	for(i = 0; i < 3; i++)
	{
		count = read(fd,&read_mode[i],sizeof(unsigned char));
		if(count < 0)
		{		
			printf("write config data error!\n"); 
			return -1;
		}

		usleep(1);
		printf("%2x \t",read_mode[i]);
	}

	printf("\n");

	return 0;
	
}


/*** converted? ***/

int IsConverted(void)
{
	int count;
	unsigned char temp;
	unsigned char next_status_read[1] = {0x40};
	unsigned char status_read[1] = {0x00};

	count = write(fd,&next_status_read[0],sizeof(unsigned char));
	if(count < 0)
	{
		printf("write cominucation data error!\n"); 
		return -1;
	}

	count = read(fd,&status_read[0],sizeof(unsigned char));
	if(count < 0)
	{
		printf("read data error!\n");
		return -1;
	}

	temp = (status_read[0] & 0x80);
	
	if(temp == 0x00)
	{
		return 0;
	}
	
	return 1;		
}


/**read data ***/

int ReadData(void)
{
	int ret;
	int i;
	int count;

	unsigned char next_data_read[1] = {0x58};
	unsigned char read_data[3] = {0x00,0x00,0x00};
DATA:
	ret = IsConverted();
	if(ret != 0)
	{
		goto DATA;
	}

	count = write(fd,&next_data_read[0],sizeof(unsigned char));
	if(count < 0)
	{
		printf("write next_config data error!\n"); 
		return -1;
	
	}
	
	
	for(i = 0; i < 3; i++)
	{
		count = read(fd,&read_data[i],sizeof(unsigned char));
		if(count < 0)
		{		
			printf("read data error!\n"); 
			return -1;
		}
		
		printf("%2x \t",read_data[i]);
	}

	printf("\n");

	return 0;				
}


int main(int argc, char *argv[])
{
	int ret;
	int count;
	int i;
	char *DEVICE_NAME = "/dev/spi";
	
	fd = open(DEVICE_NAME,O_RDWR);
	if(fd == -1)
	{
		printf("open device fail!\n");
	}

	printf("1\n");
	
	ret = InitAD7192();
	if(ret < 0)
	{
		printf("init error!\n");

		return 0;
	}

	
	ret = ReadAD7192();
	if(ret < 0)
	{
		printf("read error!\n");		
		return 0;
	}

	while(1)
	{
		ret = ReadData();
		if(ret < 0)
		{
			printf("read error!\n");
			return 0;
		}
	}

	close(fd);
	return 0;
}
